package com.ztgf.pay.api.cms;

import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.ztgf.common.constant.Result;
import com.ztgf.common.enums.CodeEnum;
import com.ztgf.common.utils.StrUtils;
import com.ztgf.pay.models.bo.PayOrderMotherSaveBO;
import com.ztgf.pay.models.bo.PayUserBO;
import com.ztgf.pay.models.bo.PayUserMoneySaveBO;
import com.ztgf.pay.models.bo.PayZjBalanceLogBO;
import com.ztgf.pay.models.dto.PayOrderDTO;
import com.ztgf.pay.models.dto.PayOrderMotherDTO;
import com.ztgf.pay.models.dto.PayUserDTO;
import com.ztgf.pay.models.dto.PayUserRechargeDTO;
import com.ztgf.pay.models.service.IPayOrderMotherService;
import com.ztgf.pay.models.service.IPayOrderService;
import com.ztgf.pay.models.service.IPayUserRechargeService;
import com.ztgf.pay.models.service.IPayUserService;
import com.ztgf.pay.models.vo.PayUserBalanceVO;
import com.ztgf.pay.models.vo.PayZjBalanceLogVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.dubbo.config.annotation.DubboReference;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 用户信息表控制器
 *
 * @author mxli
 * @Date 2020-07-30 12:13:56
 */

@RestController
@RequestMapping("/cms/admin")
@Slf4j
@Api(value = "PayCmsController",tags = "支付系统管理接口")
public class PayCmsController {

    @DubboReference
    private IPayUserService payUserService;

    @DubboReference
    private IPayUserRechargeService payUserRechargeService;

    @DubboReference
    private IPayOrderService payOrderService;


    @DubboReference
    private IPayOrderMotherService payOrderMotherService;

    @Value("${ztgf-income-user-id}")
    private Long ztgfIncomeUserId;//中铁工服收益账号

    @PostMapping("/getUserBalance")
    @ApiOperation(value = "获取用户余额",notes = "获取用户余额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userId",value = "用户id（店铺id）",required = true,paramType = "query", dataType = "Long")
            ,@ApiImplicitParam(name = "accountType",value = "账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)",required = true,paramType = "query", dataType = "Integer")
    })
    public Result<PayUserBalanceVO> getUserBalance(Long userId, Integer accountType){
        return payUserService.getUserBalance(userId, accountType);
    }

    @PostMapping("/getAdminBalance")
    @ApiOperation(value = "admin获取用户余额",notes = "admin获取用户余额")
    public Result<PayUserBalanceVO> getUserBalance1(){
        return payUserService.getUserBalance(ztgfIncomeUserId, 3);
    }

    @PostMapping("/queryPageZjBalanceLog")
    @ApiOperation(value = "获取中金当天交易流水",notes = "获取中金当天交易流水")
    public Result<Page<PayZjBalanceLogVO>> queryPageZjBalanceLog(PayZjBalanceLogBO bo){
        return payUserService.queryPageZjBalanceLog(bo);
    }

    @PostMapping("/queryPageZjHistoryBalanceLog")
    @ApiOperation(value = "获取中金历史交易流水",notes = "获取中金历史交易流水")
    public Result<Page<PayZjBalanceLogVO>> queryPageZjHistoryBalanceLog(PayZjBalanceLogBO bo){
        return payUserService.queryPageZjHistoryBalanceLog(bo);
    }

    @PostMapping("/saveBalancePay")
    @ApiOperation(value = "余额支付",notes = "余额支付")
    public Result saveBalancePay(PayOrderMotherSaveBO saveBO){
        return payOrderService.saveBalancePay(saveBO);
    }

    @PostMapping("/saveFreezeMoney")
    @ApiOperation(value = "批量冻结金额",notes = "批量冻结金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userRecharges",value = "[{}],userId:用户id,money:金额,accountType:账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)",required = true,paramType = "query", dataType = "String")
    })
    public Result<List<PayUserRechargeDTO>> saveFreezeMoney(String userRecharges){
        if(StrUtils.isEmpty(userRecharges)){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        List<PayUserMoneySaveBO> list = JSONObject.parseArray(userRecharges, PayUserMoneySaveBO.class);
        return payUserRechargeService.saveFreezeMoney(list);
    }

    @PostMapping("/saveUnfreezeMoney")
    @ApiOperation(value = "批量解冻金额",notes = "批量解冻金额")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "userRecharges",value = "[{}],userId:用户id,money:金额,accountType:账户类型( 1:客户资金账户 3:合作方收益账户 4:手续费账户)",required = true,paramType = "query", dataType = "String")
    })
    public Result<List<PayUserRechargeDTO>> saveUnfreezeMoney(String userRecharges){
        if(StrUtils.isEmpty(userRecharges)){
            return new Result<>(CodeEnum.FAIL_100002);
        }
        List<PayUserMoneySaveBO> list = JSONObject.parseArray(userRecharges, PayUserMoneySaveBO.class);
        return payUserRechargeService.saveUnfreezeMoney(list);
    }

    @ApiOperation(value = "获取用户信息表列表",notes = "获取用户信息表列表")
    @PostMapping("/queryUserPageList")
    public Result<Page<PayUserDTO>> queryUserPageList(PayUserBO bo){
        Wrapper<PayUserDTO> wrapper = new EntityWrapper<>();

        if (bo.getStatus() != null){
            wrapper.eq("status", bo.getStatus());
        }

        if(bo.getUserId() != null){
            wrapper.eq("user_id", bo.getUserId());
        }

        if(StrUtils.isNotEmpty(bo.getUserName())){
            wrapper.like("user_name", bo.getUserName());
        }
        if(StrUtils.isNotEmpty(bo.getUserName())){
            wrapper.like("legal_name", bo.getLegalName());
        }
        if(StrUtils.isNotEmpty(bo.getUserName())){
            wrapper.like("legal_credentials_no", bo.getLegalCredentialsNo());
        }
        if(StrUtils.isNotEmpty(bo.getUserName())){
            wrapper.like("business_license_no", bo.getBusinessLicenseNo());
        }
        if(bo.getAccountType() != null){
            wrapper.eq("account_type", bo.getAccountType());
        }

        if (bo.getStartDate() != null){
            wrapper.ge("create_time", bo.getStartDate());
        }
        if (bo.getEndDate() != null){
            wrapper.le("create_time", bo.getEndDate());

        }
        Page<PayUserDTO> page = new Page<>(bo.getPageNo(), bo.getPageSize());
        page = payUserService.selectPage(page, wrapper);

        return new Result<>(page);
    }

    @PostMapping("/getOnlineBankPayLog")
    @ApiOperation(value = "查询网银支付状态",notes = "查询网银支付状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "motherId",value = "motherId",required = true,paramType = "query", dataType = "Long")
    })
    public Result<String> getOnlineBankPayLog(Long motherId){
        return payOrderService.getOnlineBankPayLog(motherId);
    }


    @PostMapping("/getBalancePayLog")
    @ApiOperation(value = "保存查询余额支付状态",notes = "保存查询余额支付状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "orderId",required = true,paramType = "query", dataType = "Long")
    })
    public Result<String> getBalancePayLog(Long orderId){
        return payOrderService.getBalancePayLog(orderId);
    }

    @PostMapping("/saveOnlineBankPayLog")
    @ApiOperation(value = "保存查询网银支付状态",notes = "保存查询网银支付状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "motherId",value = "motherId",required = true,paramType = "query", dataType = "Long")
    })
    public void saveOnlineBankPayLog(Long motherId){
        PayOrderMotherDTO dto = payOrderMotherService.selectById(motherId);
        if(dto != null){
            payOrderService.saveOnlineBankPayLog(dto);
        }
    }

    @PostMapping("/saveBalancePayLog")
    @ApiOperation(value = "保存查询余额支付状态",notes = "保存查询余额支付状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "orderId",value = "orderId",required = true,paramType = "query", dataType = "Long")
    })
    public void saveBalancePayLog(Long orderId){
        PayOrderDTO dto = payOrderService.selectById(orderId);
        if(dto != null){
            payOrderService.saveBalancePayLog(dto);
        }
    }
}
